package com.atguigu.pool;

import java.util.concurrent.*;

public class ThreadPoolDemo {
    public static void main(String[] args) {
        ExecutorService threadPool=new ThreadPoolExecutor(
                Runtime.getRuntime().availableProcessors(),
                Runtime.getRuntime().availableProcessors()+1,
                1L,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
        try{
            for(int i=0;i<10;i++){
                int finalI=i;
                threadPool.execute(()->{
                    System.out.println(Thread.currentThread().getName()+"\t"+finalI+"\t客户办理业务");});
            }
        }catch (Exception e){e.printStackTrace();
        }finally {
            threadPool.shutdown();
        }
    }
    private static void defaltPool(){
        ExecutorService threadPool = Executors.newFixedThreadPool(5);//一池固定数
        //ExecutorService threadPool = Executors.newSingleThreadExecutor();//一池1线程
        //ExecutorService threadPool = Executors.newCachedThreadPool();//一池N线程

        try{
            for(int i=0;i<10;i++){
                int finalI=i;
                threadPool.execute(()->{
                    System.out.println(Thread.currentThread().getName()+"\t"+"为："+ finalI +"\t客户办理业务");
                });
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            threadPool.shutdown();
        }
    }
}
